home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / computerjanitorapp / app_tests.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  9.8 KB  |  198 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. import shutil
  6. import tempfile
  7. import unittest
  8. import computerjanitor
  9. import computerjanitorapp
  10.  
  11. class MockUI(object):
  12.     
  13.     def __init__(self, testcase, app, pm):
  14.         self.testcase = testcase
  15.  
  16.     
  17.     def run(self, options, args):
  18.         self.testcase.ui_ran = True
  19.  
  20.  
  21.  
  22. class MockPackage(object):
  23.     
  24.     def __init__(self, downloadable = False):
  25.         self.candidateDownloadable = downloadable
  26.  
  27.  
  28.  
  29. class MockApt(dict):
  30.     
  31.     def __init__(self):
  32.         self._depcache = self
  33.  
  34.     
  35.     def ReadPinFile(self, filename):
  36.         pass
  37.  
  38.     
  39.     def Cache(self):
  40.         return self
  41.  
  42.  
  43.  
  44. class MockCruft(object):
  45.     
  46.     def __init__(self, name):
  47.         self.name = name
  48.  
  49.     
  50.     def get_name(self):
  51.         return self.name
  52.  
  53.  
  54.  
  55. class ApplicationTests(unittest.TestCase):
  56.     
  57.     def setUp(self):
  58.         self.mock_apt = MockApt()
  59.         self.app = computerjanitorapp.Application(apt = self.mock_apt)
  60.         self.app.apt_cache['dash'] = MockPackage(downloadable = True)
  61.         self.app.apt_cache['gzip'] = MockPackage(downloadable = True)
  62.  
  63.     
  64.     def testSetsUpAptAttributeCorrectly(self):
  65.         self.assertEqual(self.app.apt, self.mock_apt)
  66.  
  67.     
  68.     def testSetsUpAndReturnsState(self):
  69.         self.assert_(self.app.state)
  70.  
  71.     
  72.     def testSetsUpAptCacheWhenRequested(self):
  73.         self.assertNotEqual(self.app.apt_cache, None)
  74.  
  75.     
  76.     def testSetsOptionDefaultsCorrectly(self):
  77.         (options, args) = self.app.parse_options(args = [])
  78.         self.assertEqual(args, [])
  79.         self.assertEqual(options.all, None)
  80.         self.assertEqual(options.state_file, '/var/lib/computer-janitor/state.dat')
  81.         self.assertEqual(options.no_act, None)
  82.  
  83.     
  84.     def testAcceptsDashDashAllOption(self):
  85.         (options, args) = self.app.parse_options(args = [
  86.             '--all'])
  87.         self.assertEqual(options.all, True)
  88.  
  89.     
  90.     def testAcceptsDashDashStateFileOption(self):
  91.         (options, args) = self.app.parse_options(args = [
  92.             '--state-file=foo'])
  93.         self.assertEqual(options.state_file, 'foo')
  94.  
  95.     
  96.     def testAcceptsDashDashNoActOption(self):
  97.         (options, args) = self.app.parse_options(args = [
  98.             '--no-act'])
  99.         self.assertEqual(options.no_act, True)
  100.  
  101.     
  102.     def testRunsUserInterface(self):
  103.         
  104.         def pm_class(app, plugin_dirs):
  105.             self.pm_ran = True
  106.  
  107.         
  108.         def ui_class(app, pm):
  109.             return MockUI(self, app, pm)
  110.  
  111.         self.pm_ran = False
  112.         self.ui_ran = False
  113.         self.app.run(ui_class = ui_class, plugin_manager_class = pm_class)
  114.         self.assert_(self.ui_ran)
  115.  
  116.     
  117.     def testAcceptsAptCacheWhenEssentialPackagesAreThere(self):
  118.         self.assertEqual(self.app.verify_apt_cache(), None)
  119.  
  120.     
  121.     def testRejectsAptCacheWhenDashIsMissing(self):
  122.         del self.app.apt_cache['dash']
  123.         self.assertRaises(computerjanitor.Exception, self.app.verify_apt_cache)
  124.  
  125.     
  126.     def testRejectsAptCacheWhenGzipIsMissing(self):
  127.         del self.app.apt_cache['gzip']
  128.         self.assertRaises(computerjanitor.Exception, self.app.verify_apt_cache)
  129.  
  130.     
  131.     def testRejectsAptCacheWhenDashIsNotDownloadable(self):
  132.         self.app.apt_cache['dash'].candidateDownloadable = False
  133.         self.assertRaises(computerjanitor.Exception, self.app.verify_apt_cache)
  134.  
  135.     
  136.     def testRejectsAptCacheWhenGzipIsNotDownloadable(self):
  137.         self.app.apt_cache['gzip'].candidateDownloadable = False
  138.         self.assertRaises(computerjanitor.Exception, self.app.verify_apt_cache)
  139.  
  140.     
  141.     def testSetsDefaultListOfWhitelistDirectoriesCorrectly(self):
  142.         self.assert_('/etc/computer-janitor.d' in self.app.whitelist_dirs)
  143.  
  144.     
  145.     def testReturnsEmptyWhitelistByDefault(self):
  146.         dirname = tempfile.mkdtemp()
  147.         whitelist = self.app.whitelisted_cruft(dirnames = [
  148.             dirname])
  149.         shutil.rmtree(dirname)
  150.         self.assertEqual(whitelist, [])
  151.  
  152.     
  153.     def testDoesNotMindNonExistentWhitelistDirectory(self):
  154.         dirname = tempfile.mkdtemp()
  155.         subdir = os.path.join(dirname, 'foo')
  156.         whitelist = self.app.whitelisted_cruft(dirnames = [
  157.             subdir])
  158.         shutil.rmtree(dirname)
  159.         self.assertEqual(whitelist, [])
  160.  
  161.     
  162.     def testReadsWhitelistFilesCorrectly(self):
  163.         dirname = tempfile.mkdtemp()
  164.         temp1 = os.path.join(dirname, 'foo.whitelist')
  165.         temp2 = os.path.join(dirname, 'foo.whitelist~')
  166.         file(temp1, 'w').write('deb:foo\n')
  167.         file(temp2, 'w').write('deb:bar\n')
  168.         whitelist = self.app.whitelisted_cruft(dirnames = [
  169.             dirname])
  170.         shutil.rmtree(dirname)
  171.         self.assertEqual(whitelist, [
  172.             'deb:foo'])
  173.  
  174.     
  175.     def testFindsCorrectWhitelistFilesInDotDDirectory(self):
  176.         dirname = tempfile.mkdtemp()
  177.         file(os.path.join(dirname, 'foo.whitelist'), 'w').write('deb:foo\n')
  178.         file(os.path.join(dirname, 'foo.whitelist~'), 'w').write('deb:bar\n')
  179.         list = self.app.whitelist_files([
  180.             dirname])
  181.         shutil.rmtree(dirname)
  182.         self.assertEqual(list, [
  183.             os.path.join(dirname, 'foo.whitelist')])
  184.  
  185.     
  186.     def testRemovesWhitelistedCruftCorrectly(self):
  187.         crufts = [
  188.             MockCruft('deb:foo'),
  189.             MockCruft('deb:bar')]
  190.         dirname = tempfile.mkdtemp()
  191.         file(os.path.join(dirname, 'foo.whitelist'), 'w').write('deb:foo\n')
  192.         crufts2 = self.app.remove_whitelisted(crufts, dirnames = [
  193.             dirname])
  194.         shutil.rmtree(dirname)
  195.         self.assertEqual(crufts2, crufts[1:])
  196.  
  197.  
  198.